package custom;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.qianfeng.lingshijia.R;

/**
 * Created by Administrator on 2016/1/16.
 */
public class GoodsNumberReadView extends View {
    private Bitmap shopBitmap; //购物车的图片
    private Paint paint;
    private int count =0;
    private Bitmap numberBitmap; //红色圆心图标
    private int shopWidth;
    private int numWidth;
    private int shopHeight;

    public GoodsNumberReadView(Context context) {
        super(context);
        initView(context, null, 0);
    }

    public GoodsNumberReadView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, null, 0);
    }

    public GoodsNumberReadView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs, defStyleAttr);
    }

    public void initView(Context context, AttributeSet attrs, int defStyleAttr){
        shopBitmap= BitmapFactory.decodeResource(getResources(), R.drawable.menu_shoppingcart);
        numberBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ah2);
        //获得图片的宽高
        shopWidth=shopBitmap.getWidth();
        shopHeight=shopBitmap.getHeight();

        numWidth=numberBitmap.getWidth();
        paint=new Paint();
        paint.setColor(Color.BLACK);



    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widMode=MeasureSpec.getMode(widthMeasureSpec);
        int heiMode=MeasureSpec.getMode(heightMeasureSpec);

        int measureWid=shopWidth;
        int measureHei=shopHeight;

        if(widMode == MeasureSpec.AT_MOST){

        }else if(widMode == MeasureSpec.EXACTLY){
            measureWid=MeasureSpec.getSize(widthMeasureSpec);

        }else{}
        if(heiMode ==MeasureSpec.AT_MOST){}else if(heiMode ==MeasureSpec.EXACTLY){
            measureHei=MeasureSpec.getSize(heightMeasureSpec);
        }
        setMeasuredDimension(measureWid,measureHei);
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(shopBitmap, 0, 0, paint);
        if(count>0){
            paint.setColor(Color.RED);
            canvas.drawCircle((shopWidth / 4) * (3), shopHeight / 4, shopHeight / 4, paint);
            paint.setColor(Color.WHITE);
            canvas.drawText(count + "", (shopWidth / 3) * 2, getFontHeight(22),paint);
        }
    }

    public int getFontHeight(Integer textSize) {
        if (textSize != null) {
            paint.setTextSize(textSize);
        }
        Paint.FontMetrics fm=paint.getFontMetrics();
        return (int)(fm.descent-fm.ascent);
    }

    public void changeMessCount(boolean change) {
        if (change) {
            count++;
        } else {
            count--;
        }

        if (count < 0) {
            count = 0;
        }

        invalidate();
    }
}
